﻿Partial Class Sistema_InicioCAP
    Inherits System.Web.UI.Page
#Region "VariablesDeSguridad"
    Dim PermisoEliminarRemision As Boolean
    Dim PermisoEditarRemision As Boolean
#End Region
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Page.IsPostBack = False Then
            CargarDatos()
        End If
    End Sub
    Sub CargarDatos(Optional ByVal pagina As Integer = 1, Optional ByVal ultima As Boolean = False)
        Dim bd As New BdHogaresDataContext
        Dim registrosXPagina = 20

        Dim mdocumentoidentidad As String
        Dim mnombres As String
        Dim mhistoriaclinica As String
        If String.IsNullOrEmpty(txtdocumentoidentidad.Text) Then
            mdocumentoidentidad = Nothing
        Else
            mdocumentoidentidad = txtnombres.Text
        End If


        If String.IsNullOrEmpty(txtnombres.Text) Then
            mnombres = Nothing
        Else
            mnombres = txtnombres.Text
        End If

        If String.IsNullOrEmpty(txthistoriaclinica.Text) Then
            mhistoriaclinica = Nothing
        Else
            mhistoriaclinica = txthistoriaclinica.Text
        End If

        Dim familias = bd.usp_listaCAPfiltro(mdocumentoidentidad, mnombres, mhistoriaclinica)

        If ultima = True Then
            Using cuenta = bd.usp_listaCAPfiltro(mdocumentoidentidad, mnombres, mhistoriaclinica)
                pagina = Math.Ceiling(cuenta.Count() / registrosXPagina)
            End Using
        End If
        grdResultados.DataSource = familias.Skip((pagina - 1) * registrosXPagina).Take(registrosXPagina)
        grdResultados.DataBind()
        txtPagina.Text = pagina


        Using cuenta = bd.usp_listaCAPfiltro(mdocumentoidentidad, mnombres, mhistoriaclinica)
            Dim TotalRegistros = cuenta.Count()
            lblpaginas.Text = "Pagina " & pagina & " de " & Math.Ceiling(TotalRegistros / registrosXPagina)
            lblregistros.Text = "Total registros " & TotalRegistros
        End Using



    End Sub

    Protected Sub btnNuenva_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNuenva.Click
        Response.Redirect("CAPPersonas.aspx")
    End Sub

    Protected Sub Siguiente_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Siguiente.Click
        CargarDatos(CInt(txtPagina.Text) + 1)
    End Sub

    Protected Sub lnkanterior_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkanterior.Click
        If txtPagina.Text <= 1 Then
            CargarDatos(1)
        Else
            CargarDatos(CInt(txtPagina.Text) - 1)
        End If
    End Sub

    Protected Sub btnira_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnira.Click
        If txtPagina.Text <= 1 Then
            CargarDatos(1)
        Else
            CargarDatos(CInt(txtPagina.Text))
        End If
    End Sub

    Protected Sub lnkprimero_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkprimero.Click
        CargarDatos(1)
    End Sub

    Protected Sub lnkultimo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkultimo.Click
        CargarDatos(1, True)
    End Sub

    Protected Sub btnFiltrar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnFiltrar.Click
        CargarDatos()
    End Sub

    Protected Sub btnQuitarFiltro_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnQuitarFiltro.Click
        txtdocumentoidentidad.Text = ""
        txtnombres.Text = ""
        txthistoriaclinica.Text = ""
        CargarDatos()
    End Sub
End Class


